<?php

//Retorna o endereço IP do utilizador --- Pode ser engado por live headers
function getRealIpAddr()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {
      $ip=$_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {
      $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}


//Gera uma password Aleatória
function generatePassword($length=9, $strength=0) {
    $vowels = 'aeuy';
    $consonants = 'bdghjmnpqrstvz';
    if ($strength & 1) {
        $consonants .= 'BDGHJLMNPQRSTVWXZ';
    }
    if ($strength & 2) {
        $vowels .= "AEUY";
    }
    if ($strength & 4) {
        $consonants .= '23456789';
    }
    if ($strength & 8) {
        $consonants .= '@#$%';
    }
 
    $password = '';
    $alt = time() % 2;
    for ($i = 0; $i < $length; $i++) {
        if ($alt == 1) {
            $password .= $consonants[(rand() % strlen($consonants))];
            $alt = 0;
        } else {
            $password .= $vowels[(rand() % strlen($vowels))];
            $alt = 1;
        }
    }
    return $password;
}

    function IsAlphaNumeric($String)
    {
        for($i = 0; $i < strlen($String); $i++)
        {
            if(!(   $String[$i] >= 'a' && $String[$i] <= 'z'
                ||  $String[$i] >= 'A' && $String[$i] <= 'Z'
                ||  $String[$i] >= '0' && $String[$i] <= '9'))
                return false;
        }

        return true;
    }

    function IsAlphaNumericWSpaces($String)
    {
        for($i = 0; $i < strlen($String); $i++)
        {
            if(!(   $String[$i] >= 'a' && $String[$i] <= 'z'
                ||  $String[$i] >= 'A' && $String[$i] <= 'Z'
                ||  $String[$i] >= '0' && $String[$i] <= '9'
                ||  $String[$i] == ' '))
                return false;
        }

        return true;
    }

    function IsNumeric($String)
    {
        for($i = 0; $i < strlen($String); $i++)
        {
            if(!($String[$i] >= '0' && $String[$i] <= '9'))
                return false;
        }

        return true;
    }

?>